<?php
/**
 *
 * User: wesley
 * Date: 2017/11/20
 * Time: 15:35
 * Email: wu93wesley@foxmail.com
 */

namespace app\api\controller;


class Article extends Common {
    /** 新增文章 */
    public function add_article () {
        /** 接收参数*/
        $data = $this->params;
        $data[ 'article_ctime' ] = time();
        /** 写入数据库*/
        $res = db('article')->insertGetId($data);
        if ($res) {
            $this->message(200, '新增文章成功！', $res);
        } else {
            $this->message(400, '新增文章失败！');
        }
    }

    /** 文章列表 */
    public function article_list () {
        /** 接收参数*/
        $data = $this->params;
        if (!isset($data[ 'num' ])) {
            $data[ 'num' ] = 10;
        }
        if (!isset($data[ 'page' ])) {
            $data[ 'page' ] = 1;
        }
        /** 查询数据库*/
        $where[ 'article_uid' ] = $data[ 'user_id' ];
        $where['article_isdel'] = 0;
        $count = db('article')->where($where)->count();
//        dump($count);
        $page_num = ceil($count / $data[ 'num' ]);
        $field = "article_id,article_ctime,article_title,user_nickname";
        $join = [ [ 'api_user u', 'u.user_id = a.article_uid' ] ];
        $res = db('article')->alias('a')->field($field)->join($join)->where($where)->page($data[ 'page' ], $data[ 'num' ])->select();
        if (!$res) {
            $this->message(400, '查询失败！');
        } elseif (empty($res)) {
            $this->message(200, '暂无数据！');
        } else {
            $return_data[ 'articles' ] = $res;
            $return_data[ 'page_num' ] = $page_num;
            $this->message(200, '查询成功！', $return_data);
        }
    }

    /** 文章详情 */
    public function article_detail () {
        /** 接收参数 */
        $data = $this->params;
        /** 查询数据库 */
        $where[ 'article_id' ] = $data[ 'article_id' ];
        $field = "article_id,article_ctime,article_title,article_content,user_nickname";
        $join = [ [ 'api_user u', 'u.user_id = a.article_uid' ] ];
        $res = db('article')
                    ->alias('a')
                    ->field($field)
                    ->join($join)
                    ->where($where)
                    ->find();
        $res['article_content'] = htmlspecialchars_decode($res['article_content']);
        /****** 判断结果并输出 ******/
        if (!$res){
            $this->message(400,'查询失败！');
        }else {
            $this->message(200,'查询成功！',$res);
        }
    }

    /** 更新文章 */
    public function update_article () {
        /** 接收参数*/
        $data = $this->params;
        /** 存入数据库 **/
        $res = db('article')->where('article_id',$data['article_id'])->update($data);
        if ($res !== false){
            $this->message(200, '更新文章成功！');
        }else {
            $this->message(400,'更新文章失败！');
        }
    }

    /** 删除文章 */
    public function del_article () {
        /** 接收参数*/
        $data = $this->params;
        /** 修改数据库 **/
        /** 物理删除 **/
//        $res = db('article')->delete($data['article_id']);
        /** 逻辑删除 **/
        $res = db('article')->where('article_id',$data['article_id'])->setField('article_isdel', 1);
        if ($res) {
            $this->message(200,'删除成功！');
        }else {
            $this->message(400, '删除失败！');
        }
    }
}